/** ---------------------------------------------------------------------------------
 Fichier     : JAppOptions.java
 Auteur      : Christos Kornaros, Grégory Moinat, Nicolas Muller
 Date        : 02.05.2008

 But         : Créer une fenêtre de préférences

 Remarque(s) : Une partie du code a été généré par Jigloo, utilitaire pour créer
 			 : des interfaces graphiques Swing (cf commentaire ci-dessous).

 Compilateur : jdk1.6.0_02
-----------------------------------------------------------------------------------*/
package gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.*;

import javax.swing.JButton;
import javax.swing.JCheckBox;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.text.StyleConstants;
import javax.swing.JColorChooser;

import controller.Controller;


/*
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR 
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/

/**
 * Créer une fenêtre de préférences
 * 
 * @author			Christos Kornaros, Grégory Moinat, Nicolas Muller
 * @version			1.0, 02.04.2008
 * Fichier :		JAppOptions.java
 * Remarque(s) :	Une partie du code a été généré par Jigloo, utilitaire pour créer
 			  		des interfaces graphiques Swing (cf commentaire ci-dessus).
 * Compilateur : 	jdk1.6.0_02
*/
public class JAppOptions extends javax.swing.JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JTabbedPane tabPane;
	private JPanel panGeneral;
	private JButton cmdPreprocess;
	private JPanel panPreprocess;
	private JLabel lblPreprocess;
	private JCheckBox chkTabSpaces;
	private JButton cmdNumbers;
	private JButton cmdComments;
	private JPanel panComments;
	private JButton cmdCharacters;
	private JPanel panCharacters;
	private JPanel panNumbers;
	private JButton cmdStrings;
	private JPanel panStrings;
	private JButton cmdKeywords;
	private JPanel panKeywords;
	private JLabel lblNumbers;
	private JLabel lblComments;
	private JLabel lblCharacter;
	private JLabel lblStrings;
	private JTextField txtTabSize;
	private JLabel lblTabSpaces;
	private JLabel lblTabSize;
	private JLabel lblKeywords;
	private JButton cmdCancel;
	private JButton cmdOk;
	private JPanel panButtons;
	private JPanel panColors;
	
	private Controller controller;
	
	/**
	 * Constructeur: Crée la fenêtre
	 * @param frame La fenêtre à laquelle appartient ce dialogue
	 * @param controller Le controlleur de l'application
	 */
	public JAppOptions(JFrame frame, Controller controller) {
		super(frame);
		initGUI();
		this.controller = controller;
		setFields();
		setActions();
		setLocation(frame.getLocation().x + 70, frame.getLocation().y + 70);
	}
	
	
	/**
	 * Code autogénéré par JIGLOO
	 */
	private void initGUI() {
		try {
			{
			}
			BorderLayout thisLayout = new BorderLayout();
			getContentPane().setLayout(thisLayout);
			this.setTitle("Préférences");
			this.setModal(true);
			this.setResizable(false);
			{
				panButtons = new JPanel();
				FlowLayout panButtonsLayout = new FlowLayout();
				panButtonsLayout.setAlignment(FlowLayout.RIGHT);
				panButtons.setLayout(panButtonsLayout);
				getContentPane().add(panButtons, BorderLayout.SOUTH);
				{
					cmdOk = new JButton();
					panButtons.add(cmdOk);
					cmdOk.setText("Ok");
					cmdOk.setFocusTraversalPolicyProvider(true);
				}
				{
					cmdCancel = new JButton();
					panButtons.add(cmdCancel);
					cmdCancel.setText("Annuler");
				}
			}
			{
				tabPane = new JTabbedPane();
				getContentPane().add(tabPane, BorderLayout.CENTER);
				{
					panColors = new JPanel(); 
					GridBagLayout panColorsLayout = new GridBagLayout();
					panColorsLayout.rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1};
					panColorsLayout.rowHeights = new int[] {14, 24, 24, 24, 24, 24, 24, 20};
					panColorsLayout.columnWeights = new double[] {0.0, 0.0, 0.1, 0.1};
					panColorsLayout.columnWidths = new int[] {52, 135, 7, 7};
					panColors.setLayout(panColorsLayout);
					tabPane.addTab("Coloration", null, panColors, null);
					panColors.setPreferredSize(new java.awt.Dimension(20, 20));
					{
						lblKeywords = new JLabel();
						panColors.add(lblKeywords, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						lblKeywords.setText("Mots-Clés");
					}
					{
						lblStrings = new JLabel();
						panColors.add(lblStrings, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						lblStrings.setText("Chaines de caractères");
					}
					{
						lblNumbers = new JLabel();
						panColors.add(lblNumbers, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						lblNumbers.setText("Nombres");
					}
					{
						lblCharacter = new JLabel();
						panColors.add(lblCharacter, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						lblCharacter.setText("Caractères");
					}
					{
						lblComments = new JLabel();
						panColors.add(lblComments, new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						lblComments.setText("Commentaires");
					}
					{
						panKeywords = new JPanel();
						panColors.add(panKeywords, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						panKeywords.setSize(20, 20);
						panKeywords.setPreferredSize(new java.awt.Dimension(20, 20));
					}
					{
						cmdKeywords = new JButton();
						panColors.add(cmdKeywords, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						cmdKeywords.setText("modifier");
						cmdKeywords.setFont(new java.awt.Font("Dialog",0,10));
					}
					{
						panStrings = new JPanel();
						panColors.add(panStrings, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						panStrings.setPreferredSize(new java.awt.Dimension(20, 20));
						panStrings.setSize(20, 20);
					}
					{
						cmdStrings = new JButton();
						panColors.add(cmdStrings, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						cmdStrings.setText("modifier");
						cmdStrings.setFont(new java.awt.Font("Dialog",0,10));
					}
					{
						panNumbers = new JPanel();
						panColors.add(panNumbers, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						panNumbers.setPreferredSize(new java.awt.Dimension(20, 20));
						panNumbers.setSize(20, 20);
					}
					{
						cmdNumbers = new JButton();
						panColors.add(cmdNumbers, new GridBagConstraints(3, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						cmdNumbers.setText("modifier");
						cmdNumbers.setFont(new java.awt.Font("Dialog",0,10));
					}
					{
						panCharacters = new JPanel();
						panColors.add(panCharacters, new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						panCharacters.setPreferredSize(new java.awt.Dimension(20, 20));
						panCharacters.setSize(20, 20);
					}
					{
						cmdCharacters = new JButton();
						panColors.add(cmdCharacters, new GridBagConstraints(3, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						cmdCharacters.setText("modifier");
						cmdCharacters.setFont(new java.awt.Font("Dialog",0,10));
					}
					{
						panComments = new JPanel();
						panColors.add(panComments, new GridBagConstraints(2, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						panComments.setPreferredSize(new java.awt.Dimension(20, 20));
						panComments.setSize(20, 20);
					}
					{
						cmdComments = new JButton();
						panColors.add(cmdComments, new GridBagConstraints(3, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						cmdComments.setText("modifier");
						cmdComments.setFont(new java.awt.Font("Dialog",0,10));
					}
					{
						lblPreprocess = new JLabel();
						panColors.add(lblPreprocess, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						lblPreprocess.setText("Préprocesseurs");
					}
					{
						panPreprocess = new JPanel();
						panColors.add(panPreprocess, new GridBagConstraints(2, 6, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						panPreprocess.setPreferredSize(new java.awt.Dimension(20,20));
						panPreprocess.setSize(20, 20);
					}
					{
						cmdPreprocess = new JButton();
						panColors.add(cmdPreprocess, new GridBagConstraints(3, 6, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						cmdPreprocess.setText("modifier");
						cmdPreprocess.setFont(new java.awt.Font("Dialog",0,10));
					}
				}
				{
					panGeneral = new JPanel();
					GridBagLayout panGeneralLayout = new GridBagLayout();
					tabPane.addTab("Général", null, panGeneral, null);

					panGeneralLayout.rowWeights = new double[] {0.0, 0.0, 0.0, 0.1};
					panGeneralLayout.rowHeights = new int[] {14, 24, 31, 24};
					panGeneralLayout.columnWeights = new double[] {0.0, 0.0, 0.1, 0.1};
					panGeneralLayout.columnWidths = new int[] {52, 159, 7, 7};
					panGeneral.setLayout(panGeneralLayout);
					{
						lblTabSize = new JLabel();
						panGeneral.add(lblTabSize, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						lblTabSize.setText("Longueur des tabulations");
					}
					{
						lblTabSpaces = new JLabel();
						panGeneral.add(lblTabSpaces, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						lblTabSpaces.setText("Remplacer les tabulations par des espaces");
					}
					{
						txtTabSize = new JTextField();
						panGeneral.add(txtTabSize, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						txtTabSize.setPreferredSize(new java.awt.Dimension(30, 22));
					}
					{
						chkTabSpaces = new JCheckBox();
						panGeneral.add(chkTabSpaces, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					}
				}
			}
			setSize(400, 300);
			
			// On donne le focus au bouton
			getRootPane().setDefaultButton(cmdOk);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Méthode pour charger le contenu des champs selon les réglages en cours
	 * (A utiliser par exemple à la création de la fenêtre)
	 */
	private void setFields() {
		// Coloration lexicale
		panKeywords.setBackground(StyleConstants.getForeground(controller.getKeywordAttributes()));
		panStrings.setBackground(StyleConstants.getForeground(controller.getStringAttributes()));
		panNumbers.setBackground(StyleConstants.getForeground(controller.getNumberAttributes()));
		panCharacters.setBackground(StyleConstants.getForeground(controller.getCharacterAttributes()));
		panComments.setBackground(StyleConstants.getForeground(controller.getCommentsAttributes()));
		panPreprocess.setBackground(StyleConstants.getForeground(controller.getPreprocessAttributes()));
		
		// General
		txtTabSize.setText(((Integer)controller.getTabSize()).toString());
		chkTabSpaces.setSelected(controller.isReplaceTab());
	}
	
	/**
	 * Mets en place les gestions des différents événements (clicks sur les boutons)
	 */
	private void setActions() {
		
		// Bouton Ok
		cmdOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				JAppOptions.this.setVisible(false);
				
				// On applique les styles pour les couleurs
				StyleConstants.setForeground(controller.getKeywordAttributes(), panKeywords.getBackground());
				StyleConstants.setForeground(controller.getStringAttributes(), panStrings.getBackground());
				StyleConstants.setForeground(controller.getNumberAttributes(), panNumbers.getBackground());
				StyleConstants.setForeground(controller.getCharacterAttributes(), panCharacters.getBackground());
				StyleConstants.setForeground(controller.getCommentsAttributes(), panComments.getBackground());
				StyleConstants.setForeground(controller.getPreprocessAttributes(), panPreprocess.getBackground());
				
				// On applique les styles généraux
				controller.setTabSize(Integer.valueOf(txtTabSize.getText()));
				controller.setReplaceTab(chkTabSpaces.isSelected());
				
				// On reactualise la fentre de code
				controller.reScan();
			}
		});

		// Bouton annuler
		cmdCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				JAppOptions.this.setVisible(false);
			}
		});
		
		// Bouton modifier (Mots-clés)
		cmdKeywords.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				chooseColor(panKeywords);
			}
		});
		
		// Bouton modifier (Chaine de caractères)
		cmdStrings.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				chooseColor(panStrings);
			}
		});
		
		// Bouton modifier (Nombres)
		cmdNumbers.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				chooseColor(panNumbers);
			}
		});
		
		// Bouton modifier (Caractères)
		cmdCharacters.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				chooseColor(panCharacters);
			}
		});
		
		// Bouton modifier (Commentaires)
		cmdComments.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				chooseColor(panComments);
			}
		});
		
		// Bouton modifier (Preprocesseur)
		cmdPreprocess.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				chooseColor(panPreprocess);
			}
		});
	}
	
	/**
	 * Appel le sélecteur de couleur pour modifier une des colorations lexicale
	 * @param colorPanel Le carré de couleur correspondant à l'attribut dont on veut modifier la couleur
	 */
	private void chooseColor (JPanel colorPanel) {
		Color tempColor;
		
		tempColor = JColorChooser.showDialog(this,"Modifier le couleur...", colorPanel.getBackground());
		
		// On verifie si l'utilisateur n'a pas cliqué sur annuler
		if (tempColor != null)
			colorPanel.setBackground(tempColor);
	}
}
